/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is JsWs.
 *
 * The Initial Developer of the Original Code is
 * Chris Winberry.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

/**
 * @class Virtual server paths
 * @contructor
 * @param {String} path Server-relative path to initialize with
 */
function VirtualPath (path) {
	/**
	 * Current virtual mapped path
	 * @type {String}
	 */
	this.path = "";
	this.initWithPath(path);
}

/**
 * Sets the virtual path's location
 * @param {String} path Server-relative path to initialize with
 */
VirtualPath.initWithPath = function (path) {
	if (path == null || path.length < 1)
		path = VirtualPath.virtSeparator;

	this.path = this.virtSeparator + this.virtNrml(path);
}
/** @private */
VirtualPath.prototype.initWithPath = VirtualPath.initWithPath;

/**
 * System path separator
 * @type String
 */
VirtualPath.sysSeparator = Cc["@mozilla.org/file/directory_service;1"]
	.getService(Ci.nsIProperties)
	.get('ProfD', Ci.nsILocalFile)
	.path.match('/') ? '/' : '\\';
/** @private */
VirtualPath.prototype.sysSeparator = VirtualPath.sysSeparator;

/**
 * Server/virtual path separator
 * @type String
 */
VirtualPath.virtSeparator = "/";
/** @private */
VirtualPath.prototype.virtSeparator = VirtualPath.virtSeparator;

/**
 * System's root path
 * @type String
 */
VirtualPath.sysRoot = Cc["@mozilla.org/file/directory_service;1"]
	.getService(Ci.nsIProperties)
	.get('ProfD', Ci.nsILocalFile)
	.path.match(/^([\w]\:|\/|\\)/i)[0];
/** @private */
VirtualPath.prototype.sysRoot = VirtualPath.sysRoot;

/**
 * Server's root path
 * @type String
 */
VirtualPath.virtRoot = VirtualPath.virtSeparator;
/** @private */
VirtualPath.prototype.virtRoot = VirtualPath.virtRoot;

/**
 * Appends the passed in path to the current path
 * @param {String} path Path to append
 */
VirtualPath.prototype.append = function (path) {
	var parts = this.virtNrml(this.path).split(this.virtSeparator);
	var newParts = this.virtNrml(path).split(this.virtSeparator);

	for (var i = 0; i < newParts.length; i++) {
		if (newParts[i] == "..") {
			if (parts.length > 0) {
				parts.pop();
			}
		}
		else if (newParts[i] != ".") {
			parts.push(newParts[i]);
		}
	}
	this.initWithPath(parts.join(this.virtSeparator));
}

/**
 * Creates a copy of the current VirtualPath instance
 * @returns New instance of VirtualPath
 * @type VirtualPath
 */
VirtualPath.prototype.clone = function () {
	return(new VirtualPath(this.path));
}

/**
 * Returns a VirtualPath instance of the parent folder
 * @returns New instance of VirtualPath
 * @type VirtualPath
 */
VirtualPath.prototype.parent = function () {
	var parts = this.virtNrml(this.path).split(this.virtSeparator);
	parts.pop();
	return(new VirtualPath(parts.join(this.virtSeparator)));
}

/** @private */
VirtualPath.normalize = function (path, separator) {
	if ((path instanceof Ci.nsILocalFile) | (path instanceof VirtualPath))
		path = path.path;
	return(path.replace(/(^(\/|\\)+|(\/|\\)+$)/g, "").replace(/(\/|\\)+/g, separator));
}
/** @private */
VirtualPath.prototype.normalize = VirtualPath.normalize;

/**
 * Cleans up a path and converts to using the system path seprator
 * @param {String} path Path to normalize
 * @returns System normalized path
 * @type String
 */
VirtualPath.sysNrml = function (path) { return(VirtualPath.normalize(path, VirtualPath.sysSeparator)); }
/** @private */
VirtualPath.prototype.sysNrml = VirtualPath.sysNrml;

/**
 * Cleans up a path and converts to using the server/virtual path seprator
 * @param {String} path Path to normalize
 * @returns Server/virtual normalized path
 * @type String
 */
VirtualPath.virtNrml = function (path) { return(VirtualPath.normalize(path, VirtualPath.virtSeparator)); }
/** @private */
VirtualPath.prototype.virtNrml = VirtualPath.virtNrml;
